<template>
  <!-- 新增部门的弹层 -->
  <div>
    <el-dialog
      title="新增部门"
      :visible.sync="showDialog"
      :before-close="handleClose"
    >
      <!-- 表单组件  el-form   label-width设置label的宽度   -->
      <!-- 匿名插槽 -->
      <el-form ref="deptForm" :model="formDate" :rules="rules" label-width="120px">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="formDate.name" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="formDate.code" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="formDate.manager" style="width:80%" placeholder="请选择" @focus="getEmployeeSimple">
            <el-option
              v-for="item in people "
              :key="item.id"
              :label="item.username"
              :value="item.username"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="formDate.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
        </el-form-item>
      </el-form>
      <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
      <el-row slot="footer" type="flex" justify="center">
        <!-- 列被分为24 -->
        <el-col :span="6">
          <el-button type="primary" size="small" @click="sumbit">确定</el-button>
          <el-button size="small" @click="handleClose">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { getDepartmentList, addDepartments, updateDepartments, getDepartDetail } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {

  // 需要传入一个props变量来控制 显示或者隐藏
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object,
      default: () => ({})
    }
  },

  data() {
    const checkNameRepeat = async(rule, value, callback) => {
      // console.log(value)
      const { depts } = await getDepartmentList()
      console.log(depts)
      const isRepeat = depts.filter(item => item.pid === this.treeNode.id).some(item => item.name === value)
      isRepeat ? callback(new Error(`同级部门夏已经有${value}的部门了`)) : callback()
      // console.log(isRepeat)
    }
    const checkCodeRepeat = async(rule, value, callback) => {
      const { depts } = await getDepartmentList()
      const isRepeat = depts.some(item => item.code === value && value)
      isRepeat ? callback(new Error(`组织架构中已经有部门使用${value}编码`)) : callback()
    }

    return {
      formDate: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50', trigger: 'blur' },
          { validator: checkNameRepeat, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码名称要求1-50', trigger: 'blur' },
          { validator: checkCodeRepeat, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门杰少不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门要求1-50个字符' }
        ]

      },
      people: []

    }
  },

  methods: {
    // 获取员工简单列表数据
    async getEmployeeSimple() {
      this.people = await getEmployeeSimple()
      console.log(this.people)
    },
    // 关闭
    handleClose() {
      this.formDate = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
      // 重置表单初始值,并且移除校验效果
      this.$refs.deptForm.resetFields()
      this.$emit('update:visible', false)
    },

    // 新增按钮
    async sumbit() {
      await this.$refs.deptForm.validate()
      if (this.formDate.id) {
        await updateDepartments(this.formDate)
        this.$message.success('编辑成功')
      } else {
        await addDepartments({
          ...this.formDate,
          pid: this.treeNode.id
        })
        this.$message.success('新增部门成功')
      }

      this.$emit('addDepts')
      this.handleClose()
    },
    async getDepartDetail(id) {
      this.formDate = await getDepartDetail(id)
    }

  }
}
</script>

<style>

</style>
